package cn.edu.java.io.inputstream;

import cn.edu.java.object.Person;

import java.io.*;


public class ObjectInputStreamDemo {
    public static void main(String[] args) {
        try {
            // 创建一个ObjectInputStream，从文件中读取对象
            FileInputStream fileInputStream = new FileInputStream("output/person.ser");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

            // 读取对象
            Object obj = objectInputStream.readObject();

            // 判断读取的对象是否是期望的类型
            if (obj instanceof Person) {
                Person person = (Person) obj;
                System.out.println("读取对象成功:");
                System.out.println(person);
            }

            // 关闭输入流
            objectInputStream.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


